/*
 * Copyright (c) 2014 pkzd/1.0, 颜文泽(robin)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     1 Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     2 Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef MC_BUF_H
#define MC_BUF_H

#include	<types.h>

#define NBUF	1024

#define B_WRITE		0x1	
#define B_READ		0x2
#define B_ASYNC		0x4
#define B_DONE		0x8	
#define B_ERROR		0x10
#define	B_BUSY		0x20 
#define B_DELWRI	0x40	
#define B_WANTED	0x80	
#define B_AGE		0x100

struct hd{
	uint	h_cnt;
	char	*h_addr;
};

struct buf{
	dev_t	b_dev;
	uint	b_blkno;
	uint	b_flags;
	uint	b_count;	
	char	*b_addr;
	struct hd b_ide;
	struct buf *b_forw;
	struct buf *b_back;
	struct buf *av_forw;
	struct buf *av_back;
};

struct devtab{
	uint	d_active;
	uint	d_errcnt;
	struct buf *b_forw;	
	struct buf *b_back;
	struct buf *d_actf;
	struct buf *d_actl;
};

extern struct buf buf[];
extern struct buf bfreelist;

int	binit(void);
void	clrbuf(struct buf *bp);
void	bwrite(struct buf *bp);
void	iowait(struct buf *bp);
void	brelse(struct buf *bp);
void	iodone(struct buf *bp);
void	notavail(struct buf *bp);
struct buf *bread(dev_t dev, uint blkno);
struct buf *getblk(dev_t dev, uint blkno);

#endif
